home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtog3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  4.0 KB  |  226 lines

  1. /* pbmtog3.c - read a portable bitmap and produce a Group 3 FAX file
  2. **
  3. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "g3.h"
  15.  
  16. static void tofax ARGS(( bit* bitrow, int n ));
  17. static void putwhitespan ARGS(( int c ));
  18. static void putblackspan ARGS(( int c ));
  19. static void putcode ARGS(( tableentry* te ));
  20. static void puteol ARGS(( void ));
  21. static void putinit ARGS(( void ));
  22. static void putbit ARGS(( int d ));
  23. static void flushbits ARGS(( void ));
  24.  
  25. static int reversebits;
  26.  
  27. void
  28. main( argc, argv )
  29.     int argc;
  30.     char* argv[];
  31.     {
  32.     FILE* ifp;
  33.     bit* bitrow;
  34.     int argn, rows, cols, bigcols, format, row, col, i;
  35.     char* usage = " [-reversebits] [pbmfile]";
  36.  
  37.     pbm_init( &argc, argv );
  38.  
  39.     argn = 1;
  40.     reversebits = 0;
  41.  
  42.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  43.     {
  44.     if ( pm_keymatch( argv[argn], "-reversebits", 2 ) )
  45.         reversebits = 1;
  46.     else
  47.         pm_usage( usage );
  48.     ++argn;
  49.     }
  50.     
  51.     if ( argn == argc )
  52.     ifp = stdin;
  53.     else
  54.     {
  55.     ifp = pm_openr( argv[argn] );
  56.     ++argn;
  57.     }
  58.     
  59.     if ( argn != argc )
  60.     pm_usage( usage );
  61.  
  62.     pbm_readpbminit( ifp, &cols, &rows, &format );
  63.     bigcols = max( 1728, cols );
  64.     bitrow = pbm_allocrow( bigcols );
  65.  
  66.     /* Write out four extra rows to get things stabilized. */
  67.     putinit();
  68.     for ( col = 0; col < bigcols; ++col )
  69.     bitrow[col] = PBM_WHITE;
  70.     tofax( bitrow, bigcols );
  71.     tofax( bitrow, bigcols );
  72.     tofax( bitrow, bigcols );
  73.     tofax( bitrow, bigcols );
  74.  
  75.     /* Write out bitmap. */
  76.     for ( row = 0; row < rows; ++row )
  77.     {
  78.     pbm_readpbmrow( ifp, bitrow, cols, format );
  79.     for ( col = cols; col < bigcols; ++col )
  80.         bitrow[col] = PBM_WHITE;
  81.     tofax( bitrow, cols );
  82.     }
  83.  
  84.     /* And finish off. */
  85.     for( i = 0; i < 6; ++i )
  86.     puteol( );
  87.     flushbits( );
  88.  
  89.     pm_close( ifp );
  90.  
  91.     pm_close (stdout);
  92.  
  93.     exit( 0 );
  94.     }
  95.  
  96. static void
  97. tofax(bitrow,n)
  98.     bit* bitrow;
  99.     int n;
  100. {
  101.     int c;
  102.  
  103.     while(n>0) {
  104.     c = 0;
  105.     while(*bitrow == PBM_WHITE && n>0) {
  106.         ++bitrow;
  107.         ++c;
  108.         --n;
  109.     }
  110.     putwhitespan(c);
  111.     c = 0;
  112.     if(n==0)
  113.         break;
  114.     while(*bitrow == PBM_BLACK && n>0) {
  115.         ++bitrow;
  116.         ++c;
  117.         --n;
  118.     }
  119.     putblackspan(c);
  120.     }
  121.     puteol();
  122. }
  123.  
  124. static void
  125. putwhitespan(c)
  126.     int c;
  127. {
  128.     int tpos;
  129.     tableentry* te;
  130.  
  131.     if(c>=64) {
  132.     tpos = (c/64)-1;
  133.     te = mwtable+tpos;
  134.     c -= te->count;
  135.     putcode(te);
  136.     }
  137.     tpos = c;
  138.     te = twtable+tpos;
  139.     putcode(te);
  140. }
  141.  
  142. static void
  143. putblackspan(c)
  144.     int c;
  145. {
  146.     int tpos;
  147.     tableentry* te;
  148.  
  149.     if(c>=64) {
  150.     tpos = (c/64)-1;
  151.     te = mbtable+tpos;
  152.     c -= te->count;
  153.     putcode(te);
  154.     }
  155.     tpos = c;
  156.     te = tbtable+tpos;
  157.     putcode(te);
  158. }
  159.  
  160. static void
  161. putcode(te)
  162.     tableentry* te;
  163. {
  164.     unsigned int mask;
  165.     int code;
  166.  
  167.     mask = 1<<(te->length-1);
  168.     code = te->code;
  169.     while(mask) {
  170.      if(code&mask)
  171.         putbit(1);
  172.     else
  173.         putbit(0);
  174.     mask >>= 1;
  175.     }
  176.  
  177. }
  178.  
  179. static void
  180. puteol()
  181. {
  182.     int i;
  183.  
  184.     for(i=0; i<11; ++i)
  185.     putbit(0);
  186.     putbit(1);
  187. }
  188.  
  189. static int shdata;
  190. static int shbit;
  191.  
  192. static void
  193. putinit()
  194. {
  195.     shdata = 0;
  196.     shbit = reversebits ? 0x01 : 0x80;
  197. }
  198.  
  199. static void
  200. putbit(d)
  201. int d;
  202. {
  203.     if(d) 
  204.     shdata = shdata|shbit;
  205.     if ( reversebits )
  206.     shbit = shbit<<1;
  207.     else
  208.     shbit = shbit>>1;
  209.     if((shbit&0xff) == 0) {
  210.     putchar(shdata);
  211.     shdata = 0;
  212.     shbit = reversebits ? 0x01 : 0x80;
  213.     }
  214. }
  215.  
  216. static void
  217. flushbits( )
  218. {
  219.     if ( ( reversebits && shbit != 0x01 ) ||
  220.      ( ! reversebits && shbit != 0x80 ) ) {
  221.     putchar(shdata);
  222.     shdata = 0;
  223.     shbit = reversebits ? 0x01 : 0x80;
  224.     }
  225. }
  226.